package com.probiz.estore.common.model.sales;

import java.util.Calendar;
import java.util.Date;

import com.probiz.estore.common.helper.ConfigUtil;
import com.probiz.estore.common.model.cart.ShoppingcartItemGc;
import com.probiz.estore.common.model.sales.base.GiftCertificateTbl;

/**
 * 礼券实体
 * @author ProBIZ
 *
 */
public class GiftCertificate extends GiftCertificateTbl {  
	/**
	 * 
	 */
	private static final long	serialVersionUID	= -109720087278889035L;
	public static final Short STATUS_ACTIVE = 1;//礼券激活
	public static final Short STATUS_UNACTIVE = 2;//非激活
	public static final Short STATUS_UNDEAL = 0;//未成交，刚下订单，但未支付
	
	public static final Short SENTBYEMAIL_YES = 1;//电子礼券
	public static final Short SENTBYEMAIL_NO = 0;//邮政礼券

	//前台状态
	public static final Short STATE_INVALID = 0;//无效,礼券号不存在或者其他原因
	public static final Short STATE_UNACTIVE = -1;//无效，因为礼券号未激活
	public static final Short STATE_UNDEAL = -2;//无效，因为礼券号在未成交
	public static final Short STATE_EXPIRE = -3;//无效，因为礼券号已经超过可用时间
	public static final Short STATE_NOT_ENOUGH_REMAINEDAMT = -4;//无效，因为余额不足以支付规定的金额
	public static final Short STATE_REMAINEDAMT_IS_ZERO = -5;//无效，因为余额为零
	public static final Short STATE_AVAILABLE = 1;//有效
	
	//后台状态
	public static final Short SA_STATE_ACTIVE = 1;//激活
	public static final Short SA_STATE_UNACTIVE = 2;//非激活
	public static final Short SA_STATE_UNDEAL = 0;//未成交
	public static final Short SA_STATE_PAST = 3;//已过期
	
	
	
	
  	/**
	 * Default Empty Constructor for class GiftCertificate
	 */
	public GiftCertificate () {
		super();
	}
	
	public GiftCertificate (ShoppingcartItemGc shoppingcartItemGc) {
		super();
		setCreateTime(new Date());
		setGiftCertAmt(shoppingcartItemGc.getGiftCertAmt());
		setRemainedAmt(shoppingcartItemGc.getGiftCertAmt());
		setPurchaser(shoppingcartItemGc.getPurchaser());
		setRecipient(shoppingcartItemGc.getRecipient());
		setRecipientEmail(shoppingcartItemGc.getRecipientEmail());
		setMessage(shoppingcartItemGc.getMessage());
		setIsSentByEmail(GiftCertificate.SENTBYEMAIL_YES);
		setStatus(GiftCertificate.STATUS_UNDEAL);
	}
	
	/**
	 * Default Key Fields Constructor for class GiftCertificate
	 */
	public GiftCertificate (
		 Integer in_giftCertificateId
		) {
		super (
		  in_giftCertificateId
		);
	}
	/**
	 * 获得使用状态
	 * @return
	 */
	public short getFrontState() {
		if (null == this) {
			return GiftCertificate.STATE_INVALID;
		}
		if (this.getExpireTime() != null && (new Date()).compareTo(this.getExpireTime()) > 0) {
			return GiftCertificate.STATE_EXPIRE;
		}
		if (this.getStatus().equals(GiftCertificate.STATUS_UNACTIVE)) {
			return GiftCertificate.STATE_UNACTIVE;
		}
		if (this.getStatus().equals(GiftCertificate.STATUS_UNDEAL)) {
			return GiftCertificate.STATE_UNDEAL;
		}

		return GiftCertificate.STATE_AVAILABLE;
	}
	
	/**
	 * 获得后台状态
	 * @return
	 */
	public short getState(){
		Calendar now=Calendar.getInstance();
		if(this.getExpireTime()!=null && now.getTime().compareTo(this.getExpireTime()) >= 0){
			return GiftCertificate.SA_STATE_PAST;
		}
		if(this.getStatus().equals(GiftCertificate.STATUS_ACTIVE)){
			return GiftCertificate.SA_STATE_ACTIVE;
		}else if(this.getStatus().equals(GiftCertificate.STATUS_UNACTIVE)){
			return GiftCertificate.SA_STATE_UNACTIVE;
		}else if(this.getStatus().equals(GiftCertificate.STATUS_UNDEAL)){
			return GiftCertificate.SA_STATE_UNDEAL;
		}
		return GiftCertificate.SA_STATE_UNACTIVE;
	}
	
	/**
	 * 获得默认过期时间
	 * @return
	 */
	public Date getDefaultGiftCertificateExpireTime()
    {
        int conf = ConfigUtil.getInstance().getGiftCertificateExpireYears();
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.YEAR, conf);
        cal.add(Calendar.DAY_OF_YEAR, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        return cal.getTime();
    }

	
	public boolean equals(Object object){
		return super.equals(object);
	}
	public int hashCode() {
		return super.hashCode();
	}
	

}
